package com.example.leetdemo.finash;

public class Solution303 {

    public static void main(String[] args) {
        NumArray n = new NumArray(new int[] {1, 2, 3});
        System.err.println(n.sumRange(0, 1));
        System.err.println(n.sumRange(1, 2));
    }

    public static class NumArray {
        int[] sumIndex;

        public NumArray(int[] nums) {
            sumIndex = new int[nums.length + 1];
            int sum = 0;
            for (int i = 0; i < nums.length; i++) {
                sum += nums[i];
                sumIndex[i + 1] = sum;
            }
        }

        public int sumRange(int left, int right) {
            return sumIndex[right + 1] - sumIndex[left];
        }
    }
}
